package com.example.module_marketers

import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.example.module_marketers.adapter.BusinessProfileAdapter
import com.example.module_marketers.databinding.ActivityMarketersBinding
import com.example.module_marketers.viewmodel.MarketersViewModel

/**
 * 智慧园区-04-商家推荐V1.1-张亦婷-20250509
 */
@Route(path = RouterPath.MODULE_BUSINESS_PROFILE_ACTIVITY)
class MarketersActivity : BaseMvvmActivity<ActivityMarketersBinding>() {
    lateinit var businessProfileAdapter: BusinessProfileAdapter
    private val viewModel:MarketersViewModel by viewModels()
    override fun initViews() {
        super.initViews()
        binding.back.setOnClickListener {
            finish()
        }
        businessProfileAdapter= BusinessProfileAdapter()
        binding.rvBusinessProfile.layoutManager= LinearLayoutManager(this)
        binding.rvBusinessProfile.adapter=businessProfileAdapter
        viewModel.getBusinessProfile()
        businessProfileAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(RouterPath.MODULE_BUSINESS_PROFILE_DETAIL_ACTIVITY)
                .withInt("id",businessProfileAdapter.data[position].id)
                .navigation()
        }
        binding.addBusiness.setOnClickListener {
            ARouter.getInstance().build("/module_marketers/residencyApplicationEditActivity").navigation()
        }
        binding.etSearch.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                if (p0.toString().isNotEmpty()){
                    val list=businessProfileAdapter.data.filter {
                        it.name.contains(p0.toString())
                    }
                    businessProfileAdapter.data.clear()
                    businessProfileAdapter.data.addAll(list)
                    businessProfileAdapter.notifyDataSetChanged()
                }else{
                    viewModel.getBusinessProfile()
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }
        })
    }


    override fun observeDataChange() {
        viewModel.getBusinessProfileSuccess.observe(this){
            businessProfileAdapter.data.clear()
            businessProfileAdapter.data.addAll(it)
            businessProfileAdapter.notifyDataSetChanged()
        }
    }


    override fun getLayoutId(): Int =R.layout.activity_marketers
}